package com.wits.qso.web.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wits.platform.common.utils.DateUtil;
import com.wits.platform.common.utils.PageUtil;
import com.wits.platform.web.controller.BaseController;
import com.wits.platform.web.mvc.AjaxResult;
import com.wits.qso.entity.Btable;
import com.wits.qso.entity.Student;
import com.wits.qso.entity.Vm;
import com.wits.qso.entity.VmUser;
import com.wits.qso.service.BtableDaoService;
import com.wits.qso.service.StudentService;
import com.wits.qso.service.VmService;
import com.wits.qso.service.VmUserService;

@Controller
@RequestMapping(value="/qso/monitor")
public class MonitorController extends BaseController{

	
	private static final String WIN_TYPE_FOUR="win4";
	@Autowired
	private BtableDaoService btableDaoService;
	
	@Autowired
	private VmUserService vmUserService;
	
	@Autowired
	private VmService vmService;
	
	@Autowired
	private StudentService studentService;
	
	
	
	@RequestMapping(value="toMonitor")
	public String toMonitor(){
		return "qso/monitor/main";
	}
	

	/**
	 * 监控窗口调用(如果当前没有选中的学生则随机监视)
	 * @param req
	 * @param winType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="monitorVmList")
	@ResponseBody
	public Object monitorVmList(HttpServletRequest req,@RequestParam("winType") String winType){
		List<String> selectedUserIdList=(List<String>)req.getSession().getAttribute("selectedUserIdList");
		List<VmUser> resultList=null;
		if(selectedUserIdList==null || selectedUserIdList.size()==0){
			String userId=this.getCurrentUserId();
			Btable btable=btableDaoService.findByTeaUserIdAndLessonTime(userId, DateUtil.getNow());
			if(btable!=null && btable.getClaid()!=null){
				String clsId=btable.getClaid().getClsId();
				if(WIN_TYPE_FOUR.equals(winType)){
					//查询班级下所有学生的虚拟机
					resultList= vmUserService.findByClassIdAndLimit(clsId, 4);
				}else{
					resultList= vmUserService.findByClassIdAndLimit(clsId, 9);
				}
			}

		}else{
			resultList=vmUserService.findByUserId(selectedUserIdList);
		}
		return PageUtil.toAjaxPageResult(resultList,VmUser.getJsonConfig());
	}
	
	
    @RequestMapping(value="/monitorVm/{vmId}")
    public String monitorVm(Model model,@PathVariable String vmId){
    	Vm vm=vmService.getOne(vmId);
    	model.addAttribute("vm", vm);
    	return "qso/monitor/frameMonitor";
    }
    
    @RequestMapping(value="/singleVm/{vmId}")
    public String singleVm(Model model,@PathVariable String vmId){
    	Vm vm=vmService.getOne(vmId);
    	model.addAttribute("vm", vm);
    	return "qso/monitor/singleMonitor";
    }
    

	
	@RequestMapping(value = "/monitorWin9")
	public String monitorWin9(){
		return "qso/monitor/win9";
	}	
	
	@RequestMapping(value = "/monitorWin4")
	public String monitorWin4(){
		return "qso/monitor/win4";
	}	
	
	
	/**
	 * 
	 * 老师选择监控学生(根据当前时间当前老师的查询基本课表获得班级Id)
	 * @return
	 */
	@RequestMapping(value = "/toSelect")
	public String toSelect(Model model){
		
		String userId=this.getCurrentUserId();
		Btable btable=btableDaoService.findByTeaUserIdAndLessonTime(userId, DateUtil.getNow());
		String clsId=null;
		if(btable!=null){
			clsId=btable.getClaid().getClsId();
		}
		//从班级里选择学生
		List<Student> students= studentService.findByClsId(clsId);
		model.addAttribute("students",students);
		return "qso/monitor/students";
	}
	
	
	@RequestMapping(value="/doSelect")
	@ResponseBody
	public AjaxResult doSelect(Model model,HttpServletRequest req,@RequestParam("selectIds") String selectIds){
		List<String> selectedUserIdList=new ArrayList<String>();
		Collections.addAll(selectedUserIdList,StringUtils.split(selectIds,","));
		Integer selectedSize=selectedUserIdList.size();
		req.getSession().setAttribute("selectedUserIdList",selectedUserIdList);
		return AjaxResult.success(selectedSize);
	}
}
